package com.example.wwk.graduationpractice.NetDataUtils;


import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.wwk.graduationpractice.Bean.MessageBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wwk on 2017/3/28.
 * 消息历史记录的获取
 */

public class MessagesDataUtils {
    public static final int GETMSGSUCCESS = 3;
    public static void  getMsgs(String sender, String receiver, final Handler handler) {
        String Url = "http://192.168.191.1:9090/plugins/chatlogs/getchatlogs?action=querylogs";
        Url += "&sender=" + sender + "&receiver=" + receiver+"&msgUser="+receiver;
        final String finalUrl = Url;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<MessageBean> msgs ;
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(finalUrl)
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Gson gson = new Gson();
                    msgs = gson.fromJson(responseData, new TypeToken<List<MessageBean>>() {
                    }.getType());
                    Message msg=handler.obtainMessage(GETMSGSUCCESS,msgs);
                    msg.sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public static void deleteWhoHistory(String who){
        String Url = "http://192.168.191.1:9090/plugins/chatlogs/getchatlogs?action=deletelogs";
        Url += "&msgUser="+who;
        final String finalUrl = Url;
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(finalUrl)
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d("123", "run:delete history "+responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }





}
